<?php

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
require_once ROOT_PATH . '/lib/common/SearchObject.php';

class Options {

	const TABLE_NAME = 'hs_hr_sur_options';
	
	const DB_FIELD_OPTIONS_ID = 'option_id';
	const DB_FIELD_QUESTIONS_ID = 'question_id';
	const DB_FIELD_OPTIONS_TEXT = 'option_text';
	const DB_FIELD_OPTIONS_VALUE = 'option_value';
	
	private $option_id;
	private $question_id;
	private $option_text;
	private $option_value;
	
	public function __construct($option_id = null) {
		$this->option_id = $option_id;
	}

	public function setId($id) {
		$this->option_id = $id;
	}
	
	public function setQuestionId($id) {
		$this->question_id = $id;
	}

	public function setText($option_text) {
		$this->option_text = $option_text;
	}
	
	public function setValue($value) {
		$this->option_value = $value;
	}

	public function getId() {
		return $this->option_id;
	}

	public function getQuestionID() {
		return $this->question_id;
	}
	
	public function getText() {
		return $this->option_text;
	}
	
	public function getValue() {
		return $this->option_value;
	}

    public function save() {
		if (isset($this->option_id)) {
			return $this->_update();
		} else {
			return $this->_insert();
		}
    }

	public static function delete($ids) {

		$count = 0;
		
		if (!is_array($ids)) {
			throw new OptionsException("Invalid parameter to delete(): ids should be an array", OptionsException::INVALID_PARAMETER);
		}
		
		foreach ($ids as $id) {
			if (!CommonFunctions::isValidId($id)) {
				throw new OptionsException("Invalid parameter to delete(): id = $id", OptionsException::INVALID_PARAMETER);
			}
		}
		
		if (!empty($ids)) {
			
			$sql = sprintf("DELETE FROM %s WHERE %s IN (%s)", self::TABLE_NAME,
			                self::DB_FIELD_OPTIONS_ID, implode(",", $ids));
			//print_r($sql);
			$conn = new DMLFunctions();
			$result = $conn->executeQuery($sql);
			if ($result) {
				$count = mysql_affected_rows();
			}
		}
		return $count;
	}


	public static function getAll() {
		return self::_getList();
	}
	
	public static function getOptionsByQuestions($question_id) {
		$condition[0]= "(".self::DB_FIELD_QUESTIONS_ID." = ".$question_id .")";
		return self::_getList($condition);
		
	}
	
	public static function getOptionsById($option_id) {
		$condition[0]= "(".self::DB_FIELD_OPTIONS_ID." = ".$option_id .")";
		return self::_getList($condition);
		
	}

	private static function _getList($selectCondition = null) {
		$fields[0] = self::DB_FIELD_OPTIONS_ID;
		$fields[1] = self::DB_FIELD_QUESTIONS_ID;
		$fields[2] = self::DB_FIELD_OPTIONS_TEXT;
		$fields[3] = self::DB_FIELD_OPTIONS_VALUE;
		
		$tables[0] = self::TABLE_NAME ;
		
		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->simpleSelect($tables[0],$fields,$selectCondition);
		//die($sql);
		$actList = array();

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		while ($result && ($row = mysql_fetch_assoc($result))) {
			$actList[] = self::_createFromRow($row);
		}
		return $actList;
	}

	private function _insert() {
				
		//$fields[0] = self::DB_FIELD_OPTIONS_ID;
		$fields[0] = self::DB_FIELD_QUESTIONS_ID;
		$fields[1] = self::DB_FIELD_OPTIONS_TEXT;
		$fields[2] = self::DB_FIELD_OPTIONS_VALUE;		

		//$values[0] = $this->option_id;
		$values[0] = $this->question_id;
		$values[1] = "'{$this->option_text}'";
		$values[2] = $this->option_value?$this->option_value:0;
		
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_insert = 'true';
		$sqlBuilder->arr_insert = $values;
		$sqlBuilder->arr_insertfield = $fields;

		$sql = $sqlBuilder->addNewRecordFeature2();

		$conn = new DMLFunctions();

		$result = $conn->executeQuery($sql);
		if (!$result || (mysql_affected_rows() != 1)) {
			throw new OptionsException("Insert failed. ", OptionsException::DB_ERROR);
		}

		return $this->option_id;
	}

	private function _update() {

		$fields[0] = self::DB_FIELD_OPTIONS_ID;
		$fields[1] = self::DB_FIELD_QUESTIONS_ID;
		$fields[2] = self::DB_FIELD_OPTIONS_TEXT;
		$fields[3] = self::DB_FIELD_OPTIONS_VALUE;		

		$values[0] = $this->option_id;
		$values[1] = $this->question_id;
		$values[2] = "'{$this->option_text}'";
		$values[3] = $this->option_value?$this->option_value:0;
		
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_update = 'true';
		$sqlBuilder->arr_update = $fields;
		$sqlBuilder->arr_updateRecList = $values;

		$sql = $sqlBuilder->addUpdateRecord1(0);

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		if (!$result) {
			throw new OptionsException("Update failed. SQL=$sql", OptionsException::DB_ERROR);
		}
		return $this->option_id;
	}

    private static function _createFromRow($row) {
    	
    	$option = new Options($row[self::DB_FIELD_OPTIONS_ID]);
    	$option->setQuestionId($row[self::DB_FIELD_QUESTIONS_ID]);
    	$option->setText($row[self::DB_FIELD_OPTIONS_TEXT]);    	
    	$option->setValue($row[self::DB_FIELD_OPTIONS_VALUE]);
    	return $option;
    }

}

class OptionsException extends Exception {
	const INVALID_PARAMETER = 0;
	const DB_ERROR = 1;
}
?>